<?php echo $this->render('link/header.phtml') ?>
//常用变量初始化
var menuDelUrl=baseUrl+"/link/serverkeyword/type/del";
var menuCheckUrl=baseUrl+"/link/servercheck/type/checkkeyword";
var menuEditUrl=baseUrl+"/link/serverkeyword/type/edit";
var module="keyword";
var gridTitle="关键字列表";
var dataId="keyword_id";
var selectLength=5;
var keyword = Ext.data.Record.create([
           {name: 'keyword_id', type: 'int'},
           {name: 'keyword_client_id', type: 'string'},
           {name: 'keyword_name', type: 'string'},           
		   {name: 'keyword_link', type: 'string'},
           {name: 'keyword_host', type: 'string'},
           {name: 'keyword_count', type: 'string'},
           {name: 'keyword_pr', type: 'string'}
      ]);

//避免自动定义combo内容，重新定义一个store.                                        
var client_combo_store_link_add=new Ext.data.JsonStore({
																	  url:baseUrl+'/link/serverclient/check/yes',
																	  totalProperty: 'total',
																	  root:'results',
																	  fields:['client_id','client_company','client_host']
																	});                                         
client_combo_store_link_add.load();
var client_combo=new Ext.form.ComboBox({
										width:280,
										 fieldLabel:'选择客户',
										 store:client_combo_store_link_add,										
        								displayField:'client_host',
        								valueField :'client_id',
										hiddenName:"client_id",
        								typeAhead:true,
        								triggerAction:'all',
                                        queryParam:'client_host',                                                                               
                                        //hideTrigger: true, //隐藏下拉箭头   
										loadingText: '正在加载...',
                                        pageSize:8,
                                        minChars:1,
                                        queryDelay:300,   
                                        mode :'remote',  
        								emptyText:'请选择...',                                       
        								selectOnFocus:true,
                                        resizable:true, 
                                        listeners:{
                                        	select:function(){                                        	
                                        	}
                                        }
										 }); 
var client_combo_toolbar=new Ext.form.ComboBox({
										width:280,
										 fieldLabel:'选择客户',
										 store:client_combo_store_link_add,										
        								displayField:'client_host',
        								valueField :'client_id',
										hiddenName:"client_id",
        								typeAhead:true,
        								triggerAction:'all',
                                        queryParam:'client_host',                                                                               
                                        //hideTrigger: true, //隐藏下拉箭头   
										loadingText: '正在加载...',
                                        pageSize:8,
                                        minChars:1,
                                        queryDelay:300,   
                                        mode :'remote',  
        								emptyText:'请选择...',                                       
        								selectOnFocus:true,
                                        resizable:true, 
                                        listeners:{
                                        	select:function(){
                                            if(this.value!=''){  
                                            	ds.baseParams.client_id=this.value;
                                            	ds.reload();       
                                            	}                               	
                                        	}
                                        }
										 });  

var keyword_type_trigger=new Ext.form.TwinTriggerField({
	id:'keyword_type_trigger',
    fieldLabel:'搜索关键词',
    trigger1Class:'x-form-clear-trigger',
    trigger2Class:'x-form-search-trigger',
    hideTrigger1:true,
    width:220,
    onTrigger1Click:function(){
    	if(this.hasSearch){
        	this.el.dom.value = '';
			ds.clearFilter(false);
            this.triggers[0].hide();
            this.hasSearch = false;       
        }
    },
    onTrigger2Click:function(){
    	var v = this.getRawValue();
        if(v.length < 1){
            this.onTrigger1Click();
            return;
        }        
        ds.baseParams.where="keyword_name like '%"+v+"%'";
        ds.reload();
        //alert("f");
        this.hasSearch = true;
        this.triggers[0].show();
    }
});  
keyword_type_trigger.on('specialkey', function(f, e){
            if(e.getKey() == e.ENTER){
                keyword_type_trigger.onTrigger2Click();
            }
        }, this);                                                                                                 
var ds=new Ext.data.Store({
							  proxy:new Ext.data.HttpProxy({
									url:baseUrl+'/link/serverkeyword/'
									}),
							  reader:new Ext.data.JsonReader({
									root:'results',
									totalProperty:'total',
									id:'keyword_id'
									},keyword)
	});		 
//定义列模型
var cm=new Ext.grid.ColumnModel([	
							new Ext.grid.CheckboxSelectionModel(),
                            {header:'关键字ID',dataIndex:'keyword_id',width:30,hidden:true,editor:new Ext.form.TextField({allowBlank:false})},
                            {header:'客户',dataIndex:'keyword_client_id',width:150,editor:client_combo,renderer:function(value,cell,record){                            var index=client_combo.getStore().find(client_combo.valueField,value);
                            var record=client_combo.getStore().getAt(index);                         
                           	if(record){
                            return record.data.client_host;
                            }
                            else{
                            return value;
                            }
                            }},														
							{header:'关键字',dataIndex:'keyword_name',width:150,editor:new Ext.form.TextField({allowBlank:false})},
							{header:'关键字链接',dataIndex:'keyword_link',width:300,editor:new Ext.form.TextField({allowBlank:false,value:'http://www.',vtype:'url'})},
							{header:'使用次数',dataIndex:'keyword_count',width:80},							
                            {header:'PR',dataIndex:'keyword_pr',width:80}							
	]);
cm.defaultSortable = true; 



//定义工具栏								 
var tb=new Ext.Toolbar({
							items:[
						<?php if($this->acl->isAllowed($this->role,'link','keywordDel')) {?>
                                         {
                                            id:'menuDel'+ExtId,
											text:'删除',
											iconCls:'icn-delete',
											handler:menuDel
											},
                        <? } ?>
                        <?php if($this->acl->isAllowed($this->role,'link','keywordAdd')) {?>                    
                                            {
                                            id:'menu_kewordadd'+ExtId,
											text:'添加',
											iconCls:'icn-add',
											handler:menuKeywordAdd
											},
                         <? } ?>
                        <?php if($this->acl->isAllowed($this->role,'link','keywordEdit')) {?>                    
                                            {
											id:'menu_save'+ExtId,
											text:'保存',	
											iconCls:'icn-edit',
											disabled:true,
											handler:menuSave
											},
                                            {
                                            id:'menuCheck'+ExtId,
											text:'检测选中关键词链接PR值',
                                            iconCls:'icn-search',
											handler:menuCheckSelect
                                            },
                                            "-","客户:",client_combo_toolbar,'-',"搜索关键词：",keyword_type_trigger,
                                            {
											xtype:'tbfill'
											},
                        <? } ?>
                        <?php if($this->acl->isAllowed($this->role,'link','keywordQuickAdd')) {?>                     
                                            {
                                            id:'menu_quickadd'+ExtId,
											text:'快速添加',
											iconCls:'icn-add',
											handler:menuAddQuick
										}
						<? } ?>					 
								 ]
	});			
 
function menuKeywordAdd(){
addtab("添加关键词",baseUrl+'/link/keywordadd/','link_keyword_add');
} 

function menuCheckSelect(){
	var selectRows=grid.getSelectionModel().getSelections();												
												if(selectRows.length==0){
													Ext.MessageBox.alert('提示','请选择一条数据');
													}
												else{		
													var message="";
                                                    if(selectRows.length>selectLength){
                                                    Ext.MessageBox.alert('提示','一次查询数据请不要超过'+selectLength+'条');
                                                    return;
                                                    }
													for(var i=0;i<selectRows.length;i++){
														if(i!=0){
															message+=",";
															}
														message+=selectRows[i].get(dataId);
														}  
                                                 var _gridBody=Ext.getCmp('grid_'+ExtId).body;   
                                                 _gridBody.mask('查询数据中...');
                                                 Ext.Ajax.request({
																	 url:menuCheckUrl,
																	 params:{id:message},																																		 																	success:function(response,option){
																var temp=Ext.util.JSON.decode(response.responseText);                                                  				_gridBody.unmask();
																ds.load({params:{start:0,limit:30}});
																grid.render();
																		 },
																	failure:function(response,option){
                                                                    _gridBody.unmask();
																		var temp=Ext.util.JSON.decode(response.responseText);	                                              						Ext.MessageBox.alert('waring',temp.msg);
																		}
																	 });
													}
}	
  
function  menuAdd(){

}		
function menuAddQuick(){
	
	var n = grid.getStore().getCount();// 获得总行数
                var p = new keyword({
                	keyword_id:"",
                    keyword_name:"",
                    keyword_link:"http://"
                });
                grid.stopEditing();// 停止编辑
                ds.insert(0, p);// 插入到第一行
                grid.startEditing(n, 1);// 开始编辑1单元格
                Ext.fly(grid.getView().getRow(0)).slideIn('t');
                Ext.fly(grid.getView().getRow(0)).frame('#cadaf9',2);
				Ext.getCmp('menu_save'+ExtId).setDisabled(false);
	}	
    
    
 function addtab(name,link,id){
	var main=Ext.getCmp('content');
	var tabId="tab_"+id;
	var tabLink=link;
	var tabTitle=name;
	var tab=main.getComponent(tabId);
	var subMainId='tab_'+id+'main';
    if(!tab){
        tab=main.add({
				'id':tabId,
				'title':tabTitle,
				closable:true,
				autoScroll : true ,
				layout : 'fit' ,
				border : false
				//autoLoad:{url:tabLink,scripts:true,nocache:true}
				});        
		main.setActiveTab(tab);  
		var loadmask = new Ext.LoadMask(tab.body,{msg:"页面加载中……"}) ;		
		loadmask.show() ;
		tab.load({
			url: tabLink,
			discardUrl: false,
			//nocache: true,
			timeout: 3000,
			scripts: true,
			text:'',
			callback:function(a,b,c){
				if(b){
					loadmask.hide();
					}
				}
		});
    }
	main.setActiveTab(tab);    
}	

      
<?php echo $this->render('link/foot.phtml') ?>
